package com.samuel.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.samuel.blog.bean.PageBean;
import com.samuel.blog.constant.CommonConstant;
import com.samuel.blog.controller.BaseController;
import com.samuel.blog.entity.JurisdictionEntity;
import com.samuel.blog.entity.RoleEntity;
import com.samuel.blog.service.JurisdictionService;
import com.samuel.blog.service.RoleService;
import com.samuel.blog.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author kaiji
 */
@Controller
@RequestMapping("admin")
public class RoleController extends BaseController {

	private static final long serialVersionUID = 8404275836632787227L;
	private static final Logger logger = Logger.getLogger(RoleController.class);
	private static final String PRE = "role/";
	private static final String PRE_PAGER = "admin/role_";
	
	@Resource(name = "roleService")
	private RoleService roleService;
	@Resource(name = "jurisdictionService")
	private JurisdictionService jurisdictionService;
	
	/**
	 * 进入列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value=PRE+"list"+SUFFIX, method =  RequestMethod.GET)
	public String toList(HttpServletRequest request) {
		this.configCss(request, "jurisdiction", "role");
		return PRE_PAGER+"list";
	}
	
	/**
	 * 获取列表数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value=PRE+"list"+SUFFIX, method =  RequestMethod.POST)
	@ResponseBody
	public String list(HttpServletRequest request, PageBean pageBean, HttpServletResponse response) {
		this.configCss(request, "jurisdiction", "role");
		
		PageInfo<RoleEntity> selectPage = roleService.selectPage(pageBean, null);
		this.returnTableData(pageBean, selectPage, response);
		
		return null;
	}
	
	/**
	 * 保存角色数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value=PRE+"save"+SUFFIX, method =  RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request, RoleEntity roleEntity, HttpServletResponse response) {
		this.configCss(request, "jurisdiction", "role");
		String message = "保存角色成功！";
		logger.info("保存角色<--->开始验证");
		if (null == roleEntity) {
			message = "请填写必填项！";
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
		}
		
		if (StringUtils.empty(roleEntity.getName())) {
			message = "请填写角色名称！";
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
		}
		
		// 查询角色名称是否重复
		RoleEntity role = roleService.selectByName(roleEntity.getName());
		if (null != role && !role.getRoleId().equals(roleEntity.getRoleId())) {
			message = "角色名称重复！";
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
		}
		
		logger.info("保存角色<--->结束验证");
		
		roleEntity.setCreateDate(new Date());
		roleEntity.setModifyDate(new Date());
		
		int num = roleService.insert(roleEntity);
		if (num > 0) {
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.SUCCESS_ONE, message);
		} else {
			message = "保存角色失败！";
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
		}
		return null;
	}
	
	
	/**
	 * 修改角色数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value=PRE+"update"+SUFFIX, method =  RequestMethod.POST)
	@ResponseBody
	public String update(HttpServletRequest request, RoleEntity roleEntity, HttpServletResponse response) {
		this.configCss(request, "jurisdiction", "role");
		String message = "修改角色成功！";
		logger.info("修改角色<--->开始验证");
		if (null == roleEntity) {
			message = "请填写必填项！";
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
		}
		
		if (StringUtils.empty(roleEntity.getName())) {
			message = "请填写角色名称！";
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
		}
		
		// 查询角色名称是否重复
		RoleEntity role = roleService.selectByName(roleEntity.getName());
		if (null != role && !role.getRoleId().equals(roleEntity.getRoleId())) {
			message = "角色名称重复！";
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
		}
		
		logger.info("修改角色<--->结束验证");
		
		roleEntity.setModifyDate(new Date());
		
		int num = roleService.updateById(roleEntity);
		if (num > 0) {
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.SUCCESS_ONE, message);
		} else {
			message = "修改角色失败！";
			logger.error(message);
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
		}
		return null;
	}
	
	/**
	 * 进入设置权限页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value=PRE+"jurisdiction"+SUFFIX, method = RequestMethod.GET)
	public String toJurisdiction(HttpServletRequest request, String roleId) {
		if (StringUtils.empty(roleId)) {
			request.setAttribute("message", "返回刷新页面重试！");
			return MESSAGE_PAGE;
		}
		this.configCss(request, "jurisdiction", "role");
		// 查询全部的顶级菜单
		List<JurisdictionEntity> jurisdictionList  =  jurisdictionService.selectByTop();
		request.setAttribute("jurisdictionList", jurisdictionList);
		// 查询当前这个角色的权限
		List<JurisdictionEntity> myJurisdictionList = jurisdictionService.selectByRoleId(roleId);
		request.setAttribute("myJurisdictionList", myJurisdictionList);
		request.setAttribute("roleId", roleId);
		return PRE_PAGER + "jurisdiction";
	}
	
	/**
	 * 保存角色和权限的关系
	 * @return
	 */
	@RequestMapping(value=PRE+"jurisdiction"+SUFFIX, method = RequestMethod.POST)
	public String jurisdiction(HttpServletRequest request, String roleId, HttpServletResponse response, String[] ids) {
		try {
			if (StringUtils.empty(roleId)) {
				String message = "请至少选择一项！";
				logger.error(message);
				request.setAttribute("message", message);
				return MESSAGE_PAGE;
			}
			if (null == ids || ids.length <= 0) {
				String message = "请至少选择一项！";
				logger.error(message);
				request.setAttribute("message", message);
				return MESSAGE_PAGE;
			}
			int num = roleService.saveRoleJur(roleId, ids);
			if (num > 0) {
				String message = "设置权限成功！";
				logger.info(message);
				return REDIRECT+"/admin/role/list.html";
			} else {
				String message = "保存权限失败！";
				logger.error(message);
				request.setAttribute("message", message);
				return MESSAGE_PAGE;
			}
		} catch (Exception e) {
			e.printStackTrace();
			String message = "保存权限失败！";
			logger.error(e.getMessage());
			request.setAttribute("message", message);
			return MESSAGE_PAGE;
		}
	}
	
	/**
	 * 批量删除角色
	 * @param request
	 * @param response
	 * @param ids  roleId主键集合
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = PRE+"delete"+SUFFIX, method = RequestMethod.POST)
	public String delete(HttpServletResponse response, @RequestParam("ids[]") String[] ids) {
		if (null == ids || ids.length <= 0) {
			logger.error("请至少选择一项！");
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, "请至少选择一项！");
			return null;
		}
		int num = roleService.deleteBatch(ids);
		if (num == 0) {
			this.ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, "系统删除角色失败！");
			return null;
		} else {
			this.ajax(response, CommonConstant.NoticeCode.SUCCESS_ONE, "删除成功！");
			return null;
		}
	}
}
